package com.lablzh.gdxy.service.impl;

import com.lablzh.gdxy.component.UserDetailsUtil;
import com.lablzh.gdxy.entity.activity.*;
import com.lablzh.gdxy.repository.*;
import com.lablzh.gdxy.repository.activity.*;
import com.lablzh.gdxy.repository.activity.BlogRepository;
import com.lablzh.gdxy.repository.activity.ProjectRepository;
import com.lablzh.gdxy.repository.activity.StudentTeamRepository;
import com.lablzh.gdxy.repository.activity.TeamProjectRepository;
import com.lablzh.gdxy.repository.activity.TeamRepository;
import com.lablzh.gdxy.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private UserDetailsUtil userDetailsUtil;
    @Autowired
    private ActivityPramRepository activityPramRepository;
    @Autowired
    private ProjectRepository projectRepository;
    @Autowired
    private TeamRepository teamRepository;
    @Autowired
    private StudentTeamRepository studentTeamRepository;
    @Autowired
    private TeamProjectRepository teamProjectRepository;
    @Autowired
    private BlogRepository blogRepository;
    @Autowired
    private StudentRepositoty studentRepositoty;
    @Override
    public String addActivityPram(ActivityPram activityPram) {
        if(activityPramRepository.findByActivityName(activityPram.getActivityName())!=null){
            return "该参数已存在！";
        }
        activityPramRepository.save(activityPram);
        return "添加成功！";
    }

    @Override
    public String updateActivityPram(ActivityPram activityPram) {
        activityPramRepository.save(activityPram);
        return "修改成功！";
    }

    @Override
    public ActivityPram getOneActivityPram(String activityName) {
        return activityPramRepository.findByActivityName(activityName);
    }

    @Override
    public List<ActivityPram> getActivityPram() {
        List<ActivityPram> activityPrams=activityPramRepository.findAll(Sort.by(Sort.Direction.DESC, "id"));
    //    activityPrams.sort(Comparator.comparing(ActivityPram::getActivityName));
        return activityPrams;
    }

    @Override
    public String deleteActivityPram(Integer id) {
        activityPramRepository.deleteById(id);
        return "删除成功！";
    }

    @Override
    public String addProject(Project project) {
        System.out.println(project.getCategory());
        LocalDate now=LocalDate.now();
        ActivityPram activityPram=activityPramRepository.findByActivityName(project.getCategory());
        if(now.isBefore(activityPram.getProjectBegin())) {
            return "该创活活动未开始！";
        }
        if(now.isAfter(activityPram.getProjectEnd())) {
            return "该创活活动已结束！";
        }
        if(projectRepository.findByProjectName(project.getProjectName())!=null){
            return "该项目已存在！";
        }
        //初始化
        project.setCreateTime(now);
        project.setProjectBegin(activityPram.getProjectBegin());
        project.setProjectEnd(activityPram.getProjectEnd());
        project.setStudentDeadline(activityPram.getStudentDeadline());
        project.setTeacherDeadline(activityPram.getTeacherDeadline());
        projectRepository.save(project);
        return "添加成功！";
    }

    @Override
    public String updateProject(Project project) {
        projectRepository.save(project);
        return "修改成功！";
    }

    @Override
    public Project getOneProject(Integer id) {
        return projectRepository.findById(id).orElse(null);
    }

    @Override
    public List<Project> getProject() {
        return projectRepository.findAll(Sort.by(Sort.Direction.DESC, "id"));
    }

    @Override
    public String deleteProject(Integer id) {
        Project project = projectRepository.findById(id).orElse(null);
        List<Blog> allByProject = blogRepository.findAllByProject(project.getProjectName());
        for (Blog blog : allByProject) {
            blogRepository.deleteById(blog.getId());
        }
        List<TeamProject> allByProjectName = teamProjectRepository.findAllByProjectName(project.getProjectName());
        for (TeamProject teamProject : allByProjectName) {
            teamProjectRepository.deleteById(teamProject.getId());
        }
        projectRepository.deleteById(id);
        return "删除成功！";
    }

    @Override
    public List<Project> getProjectTeacher() {
        String username=userDetailsUtil.getUserDetails();
        return projectRepository.findAllByTeacherUsername(username,Sort.by(Sort.Direction.DESC, "id"));
    }

    @Override
    public String updateProjectAudit(Integer id, String audit) {
        projectRepository.updateAuditById(id,audit);
        return "修改成功！";
    }

    @Override
    public String addTeam(Team team) {
        String username=userDetailsUtil.getUserDetails();
        if(teamRepository.findByTeamName(team.getTeamName())!=null){
            return "该团队已存在！";
        }
        StudentTeam studentTeam=new StudentTeam();
        studentTeam.setTeamName(team.getTeamName());
        studentTeam.setStudentUsername(username);
        studentTeam.setStudentName(studentRepositoty.findAllByStID(username).getName());
        studentTeam.setAudit("已审");
        studentTeam.setIdentity("队长");
        studentTeamRepository.save(studentTeam);
        teamRepository.save(team);
        return "添加成功！";
    }

    @Override
    public String updateTeam(Team team) {
        Team team1 = teamRepository.findById(team.getId()).orElse(null);
        if(team1==null){
            return "无法找到要修改队伍";
        }
        if(!team1.getTeamName().equals(team.getTeamName())){
            return "队名无法修改！";
        }
        teamRepository.save(team);
        return "修改成功！";
    }

    @Override
    public Team getOneTeam(Integer id) {
        return teamRepository.findById(id).orElse(null);
    }

    @Override
    public List<Team> getTeam() {
        return teamRepository.findAll();
    }

    //删除团队
    @Override
    public String deleteTeam(Integer id) {
        Team team = teamRepository.findById(id).orElse(null);
        List<TeamProject> allByTeamName = teamProjectRepository.findAllByTeamName(team.getTeamName());
        for (TeamProject teamProject : allByTeamName) {
            teamProjectRepository.deleteById(teamProject.getId());
        }
        //删除该队伍所有成员
        studentTeamRepository.deleteAllByTeamName(teamRepository.findById(id).orElse(null).getTeamName());
        //删除该队伍
        teamRepository.deleteById(id);
        return "删除成功！";
    }

    @Override
    public List<Team> getTeamLeader() {
        String username=userDetailsUtil.getUserDetails();
        return teamRepository.findAllByCaptainUsername(username);
    }
    @Override
    public List<Team> getTeamJoin() {
        String username=userDetailsUtil.getUserDetails();
        List<Team> teams=new ArrayList<>();
       List<StudentTeam> studentTeams= studentTeamRepository.findAllByStudentUsername(username);
       if(studentTeams!=null){
           for(StudentTeam studentTeam:studentTeams){
               teams.add(teamRepository.findByTeamName(studentTeam.getTeamName()));
           }
       }
       return teams;
    }


    @Override
    public String addStudentTeam(String teamName,String introduction) {
        String username=userDetailsUtil.getUserDetails();
        if(studentTeamRepository.findByTeamNameAndStudentUsername(teamName,username)!=null){
            return "您已经申请！";
        }
        StudentTeam studentTeam=new StudentTeam();
        studentTeam.setTeamName(teamName);
        studentTeam.setStudentUsername(username);
        studentTeam.setStudentName(studentRepositoty.findAllByStID(username).getName());
        studentTeam.setAudit("未审");
        studentTeam.setIntroduction(introduction);
        studentTeam.setIdentity("队员");
        studentTeamRepository.save(studentTeam);
        return "添加成功！";
    }

    @Override
    public String deleteStudentTeam(Integer id) {
        studentTeamRepository.deleteById(id);
        return "删除成功！";
    }


    @Override
    public String deleteStudentTeamByTeamName(String teamName) {
        String username=userDetailsUtil.getUserDetails();
        studentTeamRepository.deleteAllByTeamNameAndStudentUsername(teamName,username);
        return "删除成功！";
    }



    @Override
    public List<StudentTeam> getStudentTeamByTeam(String teamName) {
        return studentTeamRepository.findAllByTeamName(teamName);
    }

    @Override
    public String updateStudentTeamAudit(Integer id,String audit) {
        studentTeamRepository.updateAuditById(id,audit);
        return "修改成功！";
    }

    @Override
    public String addTeamProject(String teamName, String projectName) {
        if("".equals(teamName)) {
            return "请不要输入空的团队名！";
        }
        if(teamProjectRepository.findByTeamNameAndProjectName(teamName,projectName)!=null){
            TeamProject byTeamNameAndProjectName = teamProjectRepository.findByTeamNameAndProjectName(teamName, projectName);
            if(byTeamNameAndProjectName.getAudit().equals("未通过"))
            {
                teamProjectRepository.updateAuditById(byTeamNameAndProjectName.getId(),"未审");
                return "申请成功";
            }
            return "您已经申请！";
        }
        List<TeamProject> allByTeamName = teamProjectRepository.findAllByTeamName(teamName);
        for (TeamProject teamProject : allByTeamName) {
            if("已通过".equals(teamProject.getAudit()))
            {
                return "您已经参加其他项目！";
            }
        }
        //判断时间
        LocalDate now=LocalDate.now();
      //  LocalDate ddl=activityPramRepository.findByActivityName(projectRepository.findByProjectName(projectName).getCategory()).getStudentDeadline();
       LocalDate ddl=projectRepository.findByProjectName(projectName).getStudentDeadline();
        if(now.isAfter(ddl)) {
            return "学生选择项目已过截止时间！";
        }
        //初始化
        TeamProject teamProject=new TeamProject();
        teamProject.setTeamID(teamRepository.findByTeamName(teamName).getId());
        teamProject.setTeamName(teamName);
        teamProject.setProjectName(projectName);
        teamProject.setAudit("未审");
        teamProjectRepository.save(teamProject);
        return "申请成功！";
    }

    @Override
    public String updateTeamProjectAudit(Integer id, String audit) {
        TeamProject teamProject=teamProjectRepository.findById(id).orElse(null);
        assert teamProject != null;
        Team team=teamRepository.findByTeamName(teamProject.getTeamName());
       teamProjectRepository.updateAuditById(id,audit);
        if("已通过".equals(audit)){
            projectRepository.updateTeamIDAndTeamNameByProjectName(teamProject.getProjectName(),team.getId(),team.getTeamName());
            List<TeamProject> allByProjectName = teamProjectRepository.findAllByProjectName(teamProject.getProjectName());
            for (TeamProject project : allByProjectName) {
                if(project.getId().equals(id))
                    continue;
                teamProjectRepository.updateAuditById(project.getId(),"未通过");
            }

            List<TeamProject> allByTeamName = teamProjectRepository.findAllByTeamName(team.getTeamName());
            for (TeamProject teamProject1 : allByTeamName) {
                if(teamProject1.getId().equals(id))
                    continue;
                teamProjectRepository.deleteById(teamProject1.getId());
            }
        }
        return "修改成功！";
    }

    @Override
    public List<TeamProject> getTeamProject(String projectName) {
        return teamProjectRepository.findAllByProjectName(projectName);
    }

    @Override
    public List<Blog> getAllBlog() {
        return blogRepository.findAll();
    }

    @Override
    public String deleteBlog(int id) {
        blogRepository.deleteById(id);
        return "删除成功";
    }

    @Override
    public String addBlog(Blog blog) {
        LocalDateTime now=LocalDateTime.now();
        blog.setPublishTime(now);
        if("".equals(blog.getTheme())){
            blog.setTheme(blog.getTitle());
        }
//        blog.setSender(studentRepositoty.findAllByStID(username).getName());
        blogRepository.save(blog);
        return "添加成功！";
    }

    @Override
    public Blog getOneBlog(Integer id) {
        return blogRepository.findById(id).orElse(null);
    }

    @Override
    public List<Blog> getBlogByPorjectName(String projectName) {
        return blogRepository.findAllByProject(projectName);
    }

    @Override
    public List<Blog> getBlogByTheme(String theme) {
        return blogRepository.findAllByTheme(theme);
    }

    @Override
    public List<Project> getProjectByAudit(String audit) {
        return projectRepository.findByAudit(audit,Sort.by(Sort.Direction.DESC,"id"));
    }


}
